from flask import Flask, request, jsonify

app = Flask(__name__)


@app.route('/product', methods=['POST'])
def calculate_money():
    # 使用 request.form 来获取表单提交的数据,对应data
    #request.get_json()对应json   / 否则 / 400错误
    product_id = request.form.get('id')
    price_str = request.form.get('price')
    amount_str = request.form.get('amount')

    # 检查参数是否存在
    if not price_str or not amount_str:
        return jsonify({
            "error": "Missing 'price' or 'amount' in request"
        }), 400

    try:
        price = int(price_str)
        amount = int(amount_str)
    except ValueError:
        return jsonify({
            "error": "Both 'price' and 'amount' must be valid integers"
        }), 400

    money = price * amount

    # 返回响应
    return jsonify({
        'id': product_id,
        'price': price,
        'amount': amount,
        'money': money
    })


if __name__ == '__main__':
    app.run(debug=True)
